package ua.epam.java_bravo.controller.servletCommand;

import ua.epam.java_bravo.manager.ConfigurationManager;
import ua.epam.java_bravo.manager.MessageManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 15.12.2010
 * Time: 15:18:04
 */
public class LoginCommand implements ICommand{
    //loggins when login == pass


    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = null;

        String login = request.getParameter("login_username");
        String pass = request.getParameter("login_password");

        if ((login != null) && login.equals(pass)) {
            page = ConfigurationManager.getInstance().
                    getProperty(ConfigurationManager.ADDR_FORM_PATH);
        } else {
            page = ConfigurationManager.getInstance().
                    getProperty(ConfigurationManager.ERROR_PAGE_PATH);

            request.setAttribute("errorMessage", MessageManager.getInstance().getProperty(
                    MessageManager.LOGIN_ERROR_MESSAGE
            ));
        }

        //redirects to .jspx
        return page;
    }
}
